home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / xdsub.c < prev   
C/C++ Source or Header  |  2000-05-18  |  2KB  |  112 lines

  1. /*
  2.  
  3.                Hex dump utility
  4.  
  5.                 by John Walker
  6.            WWW home page: http://www.fourmilab.ch/
  7.  
  8.         This program is in the public domain.
  9.  
  10. */
  11.  
  12. #ifdef HEXDUMP
  13.  
  14. #include <stdio.h>
  15.  
  16. #define EOS     '\0'
  17.  
  18. static char addrformat[80] = "%6X";
  19. static char dataformat1[80] = "%02X";
  20. static int bytesperline = 16, doublechar = 0,
  21.        dflen = 2;
  22. static unsigned long fileaddr;
  23. static unsigned char lineecho[32];
  24.  
  25. /*  OUTLINE  --  Edit a line of binary data into the selected output
  26.          format.  */
  27.  
  28. static void outline(out, dat, len)
  29.   FILE *out;
  30.   unsigned char *dat;
  31.   int len;
  32. {
  33.     char oline[132];
  34.     int i;
  35.  
  36.     sprintf(oline, addrformat, fileaddr);
  37.     strcat(oline, ":");
  38.     for (i = 0; i < len; i++) {
  39.     char outedit[80];
  40.  
  41.     sprintf(outedit, dataformat1, dat[i]);
  42.         strcat(oline, (i == (bytesperline / 2)) ? "  " : " ");
  43.     strcat(oline, outedit);
  44.     }
  45.  
  46.     if (doublechar) {
  47.     char oc[2];
  48.     int shortfall = ((bytesperline - len) * (dflen + 1)) +
  49.             (len <= (bytesperline / 2) ? 1 : 0);
  50.  
  51.     while (shortfall-- > 0) {
  52.             strcat(oline, " ");
  53.     }
  54.     oc[1] = EOS;
  55.         strcat(oline, " | ");
  56.     for (i = 0; i < len; i++) {
  57.         int b = dat[i];
  58.  
  59.             /* Map non-printing characters to "." according to the
  60.            definitions for ISO 8859/1 Latin-1. */
  61.  
  62.             if (b < ' ' || (b > '~' && b < 145)
  63.             || (b > 146 && b < 160)) {
  64.                 b = '.';
  65.         }
  66.         oc[0] = b;
  67.         strcat(oline, oc);
  68.     }
  69.     }
  70.     strcat(oline, "\n");
  71.     fputs(oline, out);
  72. }
  73.  
  74. /*  XD    --  Dump a buffer.
  75.  
  76.         xd(out, buf, bufl, dochar);
  77.  
  78.         out     FILE * to which output is sent.
  79.         buf     Address of buffer to dump.
  80.         bufl    Buffer length in bytes.
  81.         dochar  If nonzero, show ASCII/ISO characters
  82.             as well as hexadecimal.
  83.  
  84. */
  85.  
  86. void xd(out, buf, bufl, dochar)
  87.   FILE *out;
  88.   unsigned char *buf;
  89.   int bufl, dochar;
  90. {
  91.     int b, bp;
  92.  
  93.     bp = 0;
  94.     fileaddr = 0;
  95.     doublechar = dochar;
  96.  
  97.     while (bufl-- > 0) {
  98.     b = *buf++;
  99.     if (bp >= bytesperline) {
  100.         outline(out, lineecho, bp);
  101.         bp = 0;
  102.         fileaddr += bytesperline;
  103.     }
  104.     lineecho[bp++] = b;
  105.     }
  106.  
  107.     if (bp > 0) {
  108.     outline(out, lineecho, bp);
  109.     }
  110. }
  111. #endif 
  112.